const Model = require('../models/order-model')
const DeviceModel = require('../models/device-model')
const ResponseModel = require('../models/response-model')


const sleep = require('../utils/common').sleep
const getDevice = require('../utils/device').getDevice

module.exports = {

    // 查询
    'get::/order/list': async function(ctx) {

        const { groupId } = ctx.request.query
        
        const whereBuilder = countQuery => {
            if (groupId) {
                countQuery.where('group_id', groupId)
            }
        }
        const list = await Model.list(Model.buildPaging(ctx.request.query), whereBuilder)
        ctx.response.body = ResponseModel.success(list)
    },
    // 保存
    'post::/order/save': async function(ctx) {

        const model = ctx.request.body

        if (model.id) {
            const updated = await Model.updateBy({
                name: model.name
            }, { id: model.id })
        } else {
            const inserted = await Model.insert(model)
            if (inserted && inserted.length > 0) {
                id = inserted[0]
                model.id = inserted[0]
            }
        }

        ctx.response.body = ResponseModel.success()
    },
    // 删除
    'get::/order/delete/:id': async function(ctx) {

        const { id } = ctx.request.params
        if (id) {
            const deleted = await Model.delete({ id: id })
        }
        ctx.response.body = ResponseModel.success()
    },

    // --- 设备对接 ---

    // 学习红外 长红外1024 短红外512
    'post::/order/learn': async function(ctx) {

        const { id, orderType } = ctx.request.body
        
        let data = null
        let message = ''
        if(id) {
            const model = await Model.getById(id)
            if (model && model.deviceId) {
                const deviceModel = await DeviceModel.getById(model.deviceId)
                if (deviceModel && deviceModel.ssid) {
                    const device = getDevice(deviceModel)
                    device.stopRedLearn()
                    await sleep(1000)
                    if (orderType == 0) {
                        // 长红外
                        device.startLongRedLearn()
                        await sleep(8000)

                        device.getLongRedLearn()
                        await sleep(2000)
                    } else {
                        // 短红外
                        device.startShortRedLearn()
                        await sleep(8000)

                        device.getShortRedLearn()
                        await sleep(2000)

                    }

                    message = device.lastResponse.message
                    data = device.lastResponse.data

                    if (device.lastResponse.status === 1 && data) {
                        await Model.updateBy({
                            code: data.join(',')
                        }, { id: id })
                    }
                }
            }
        }

        ctx.response.body = data ? ResponseModel.success(data, message) : ResponseModel.failure('无学习结果')
    },
    // 发送红外
    'get::/order/send': async function(ctx) {

        const { id } = ctx.request.query
        
        if(id) {
            const model = await Model.getById(id)
            if (model.deviceId && model.code) {
                const deviceModel = await DeviceModel.getById(model.deviceId)
                if (deviceModel && deviceModel.ssid) {
                    const device = getDevice(deviceModel)
                    const code = model.code.split(',').map(c => parseInt(c))
                    if (code.length > 512) {
                        device.sendLongRed(code)
                    } else {
                        device.sendShortRed(code)
                    }
                    // 无响应反馈数据,仅发射设备的蜂鸣器响一声
                }
            }
        }

        ctx.response.body = ResponseModel.success()
    },
}
  